import * as constants from './constants';
// import { fromJS } from 'immutable';
import {reqLogList, reqDownloadFile, downloadParsFile, deleteFile} from '../../../api'
import {downloadFile} from '../../../common/fileUtils'

const logList = (result, nextPage) => ({
    type: constants.LOG_LIST,
    logInfo: result,
    nextPage
});

const updateLoading = (loading) => ({
    type: constants.REQ_LOADING,
    loading: loading
});

export function downloadLogFile(json, func) {
    return async (dispatch) => {
        dispatch(updateLoading(true));
        const result = await reqDownloadFile(json);
        dispatch(updateLoading(false));
        downloadFile(json.fileName, result);
        func('源文件下载成功');
    };
}

export function downloadLogParsFile(json, func) {
    return async (dispatch) => {
        dispatch(updateLoading(true));
        const result = await downloadParsFile(json);
        dispatch(updateLoading(false));
        downloadFile(json.fileName + '.log', result);
        func('解析文件下载成功');
    };
}

export function readLogParsFile(json, func, resultFunc) {
    return async (dispatch) => {
        dispatch(updateLoading(true));
        const result = await downloadParsFile(json);
        dispatch(updateLoading(false));
        resultFunc(result);
        func('预览成功');
    };
}

export function deleteLogFile(json, func) {
    return async (dispatch) => {
        dispatch(updateLoading(true));
        await deleteFile(json);
        dispatch(updateLoading(false));
        func('文件删除成功');
    };
}

export const getLogFileList = (params, func) => {
    return async (dispatch) => {
        dispatch(updateLoading(true));
        const result = await reqLogList(params);
        dispatch(updateLoading(false));
        if (result.code === 0) {
            dispatch(logList(result.data));
            func('加载成功');
        }
    }
};
